package com.company;

import org.omg.PortableInterceptor.INACTIVE;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by v on 17-5-9.
 */
public class Input {
    private static String path="src/test";
    private static HashMap<String,LinkedList<String>> map=new HashMap<>(10000);

    //一次性把文件的内容全部读入到内存中，并且转换成一个String
    private static String readAll() throws IOException{
        DataInputStream input=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));

        byte[] bytes=new byte[input.available()];
        input.read(bytes);
        String str=new String(bytes);

        input.close();
        return str;
    }

    //把读入的String转化为一个易于处理的哈希表
    public static HashMap<String,LinkedList<String>> map()throws IOException{
        String str=readAll();

        Scanner input=new Scanner(str);
        while (input.hasNext()){
            //读取一行
            String line=input.nextLine();
            //处理这一行
            int index=line.indexOf(',');
            String str1=line.substring(0,index);
            String str2=line.substring(index+1);

            if(map.containsKey(str1)){
                map.get(str1).add(str2);
            }else {
                LinkedList<String> list=new LinkedList<>();
                list.add(str2);
                map.put(str1,list);
            }
        }

        return map;
    }

}
